<?php


namespace app\lunwen\controller;


use app\lunwen\model\StuTopicLogModel;
use app\lunwen\model\StuTopicModel;
use cmf\controller\HomeBaseController;

class StuModifyTitleController extends HomeBaseController
{
        public function index(){
            $value = session('stu_name');
            $this->assign("stu_name", $value);
            return $this->fetch(':stuModifyTitle');
        }

        public function modify(){
            $value = session('stu_name');
            $stuId = session('stu_id');
            //echo $value;
            //echo $stuId;

            //查询出日志表中 该用户最新的 状态
            $stuTopicLog = new StuTopicLogModel();
            $date = $stuTopicLog->where('stu_name',$value)
                                ->where('stu_id',$stuId)
                                ->limit(1)
                                ->order('create_time', 'desc')
                                ->value('status');
            //echo $date;
            //如果状态为3、5 可以修改课题
            if ($date=="导师不通过"||$date=="院系不通过"){

                $stuTopic = new StuTopicModel();
                $stuTopic->where('stu_name',$value)
                            ->where('stu_id',$stuId)
                            ->update([
                                'stu_title' => $_POST["title"],
                                'status' => '修改课题'
                            ]);

                //加入日志表
                $stutopiclog2 = new StuTopicLogModel();
                $stutopiclog2->stu_name=$value;
                $stutopiclog2->title=$_POST["title"];
                $stutopiclog2->status='修改课题';
                $stutopiclog2->stu_id=$stuId;
                $stutopiclog2->stu_teacher=$_POST["teacher"];
                //$stutopiclog2->create_time = time();
                $stutopiclog2->save();

                $titleList = new StuTopicLogModel();
                $data = $titleList->where('stu_name',$value)
                    ->whereTime('create_time','>',session('beforeYear'))
                    ->whereTime('create_time','<',session('afterYear'))
                    ->all();
                return json([
                    'code'=>1,
                    'msg'=>'修改成功',
                    'data'=>$data
                ]);
            }
            else{
                //$this->assign("msg", "现在不能修改课题");

                $titleList = new StuTopicLogModel();
                $data = $titleList->where('stu_name',$value) ->whereTime('create_time','>',session('beforeYear'))
                    ->whereTime('create_time','<',session('afterYear'))
                    ->all();
                return json([
                    'code'=>-1,
                    'msg'=>'现在不能修改课题',
                    'data'=>$data
                ]);
            }

        }
}